//
//  MyViewController.m
//  TravelEveryDay
//
//  Created by 路洪宇 on 15/10/19.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "MyViewController.h"
#import "Play_LocationTableViewCell.h"
#import "MyCollectionViewController.h"
#import "ToolViewController.h"
#import "ToolDetailViewController.h"
#import "MyHeaderView.h"
#import "AboutUsViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)MyHeaderView *headerView;
@property(nonatomic, strong)NSArray *imageArray;
@end

@implementation MyViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _choseArr = @[@"我的收藏",@"清除缓存",@"实时帮手",@"关于我们"];
     self.imageArray = @[@"me_favourite@2x.png",@"me_history@2x.png",@"me_setting@2x.png",@"me_draft@2x.png"];
    self.view.backgroundColor =[UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    [self setUpTableView];
    
     self.headerView = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT-50)/2)];
    UIImageView *userFace = [[UIImageView alloc] initWithFrame:CGRectMake(self.headerView.frame.size.width/2-30, self.headerView.frame.size.height/2-60, 60, 60)];
    [userFace setImage:[UIImage imageNamed:@"play-user.png"]];
    userFace.layer.cornerRadius = 30;
    userFace.layer.masksToBounds = YES;
    [self.headerView addSubview:userFace];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, userFace.frame.size.height+userFace.frame.origin.y+20, SCREEN_WIDTH, 30)];
    [nameLabel setText:@"－· 记录旅行的足迹, 丰富世界的风景 ·－"];
    [nameLabel setTextAlignment:NSTextAlignmentCenter];
    [nameLabel setTextColor:[UIColor whiteColor]];
    [nameLabel setFont:[UIFont systemFontOfSize:17.]];
    [self.headerView addSubview:nameLabel];
    
    [self.myTableView setTableHeaderView:self.headerView];
}

-(void)setUpTableView
{
    _myTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.showsVerticalScrollIndicator = NO;
    _myTableView.tableFooterView = [[UIView alloc] init];
   [_myTableView setSeparatorColor:[UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1]];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    _myTableView.tableFooterView = footerView;
    
    UILabel *copyrightLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 20, 200, 20)];
    [copyrightLabel setText:[NSString stringWithFormat:@"简·行iPhone版"]];
    [copyrightLabel setTextAlignment:NSTextAlignmentCenter];
    copyrightLabel.font = [UIFont systemFontOfSize:13.];
    copyrightLabel.textColor = [UIColor lightGrayColor];
    [footerView addSubview:copyrightLabel];
    
      UILabel *bbLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, copyrightLabel.frame.size.height+20, 200, 20)];
    [bbLabel setText:[NSString stringWithFormat:@"版本1.0"]];
    [bbLabel setTextAlignment:NSTextAlignmentCenter];
    bbLabel.font = [UIFont systemFontOfSize:13.];
    bbLabel.textColor = [UIColor lightGrayColor];
    [footerView addSubview:bbLabel];
    
      UILabel *coLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, bbLabel.frame.size.height+bbLabel.frame.origin.y, 200, 20)];
    [coLabel setText:[NSString stringWithFormat:@"Copyright©2015 CWL.inc"]];
    [coLabel setTextAlignment:NSTextAlignmentCenter];
    coLabel.font = [UIFont systemFontOfSize:13.];
    coLabel.textColor = [UIColor lightGrayColor];
    [footerView addSubview:coLabel];
    [self.view addSubview:_myTableView];
}

#pragma mark- TableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _choseArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [_choseArr objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.imageView.frame = CGRectMake(0, 0, 20, 20);
    cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            NSLog(@"我的收藏");
            MyCollectionViewController *collectionVC = [[MyCollectionViewController alloc] init];
            [self.navigationController pushViewController:collectionVC animated:YES];
        }
            break;
        case 1:
        {
            NSLog(@"清除缓存");
            NSString *modelPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
            NSString *cashesPath = [NSString stringWithFormat:@"%@/Caches", modelPath];
            NSString *string = [NSString stringWithFormat:@"清除缓存(%.2fM)",[self floatWithPath:cashesPath]];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            alertView.delegate = self;
            [alertView show];

        }
            break;
        case 2:
        {
            NSLog(@"工具箱");
//            ToolDetailViewController *toolVC = [[ToolDetailViewController alloc] init];
//            [self.navigationController pushViewController:toolVC animated:YES];
            ToolViewController *toolVC = [[ToolViewController alloc] init];
            [self.navigationController pushViewController:toolVC animated:YES];
        }
            break;
        case 3:
        {
            NSLog(@"关于我们");
            AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
            break;
        default:
            break;
    }
}
#pragma mark- AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (0 == buttonIndex)
    {
    }else{
        [self removeCache];
    }
}

// 删除缓存
- (void)removeCache
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
    for (NSString *p in files) {
        NSString * path = [NSString stringWithFormat:@"%@/%@", cachePath, p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
        }
    }
}
// 计算清除的缓存大小
- (CGFloat)floatWithPath:(NSString *)path
{
    CGFloat num = 0;
    NSFileManager *man = [NSFileManager defaultManager];
    if ([man fileExistsAtPath:path]) {
        NSEnumerator *childFile = [[man subpathsAtPath:path] objectEnumerator];
        NSString *fileName;
        while ((fileName = [childFile nextObject]) != nil) {
            NSString *fileSub = [path stringByAppendingPathComponent:fileName];
            num += [self fileSizeAtPath:fileSub];
        }
    }
    return num / (1024.0 * 1024.0);
}

//计算单个文件大小
- (long long)fileSizeAtPath:(NSString *)file
{
    
    NSFileManager *man = [NSFileManager defaultManager];
    if ([man fileExistsAtPath:file]) {
        return [[man attributesOfItemAtPath:file error:nil] fileSize];
    }
    return 0;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.headerView updateHeaderView:scrollView.contentOffset];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
